package com.example.xd.mvp_project.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.xd.mvp_project.R;
import com.example.xd.mvp_project.base.BaseActivity;
import com.example.xd.mvp_project.base.BaseFragment;
import com.example.xd.mvp_project.view.fragment.FragmentFactory;
import com.example.xd.mvp_project.view.fragment.MainFg;
import com.example.xd.mvp_project.view.fragment.MyFg;
import com.umeng.socialize.UMShareAPI;

public class MainActivity extends BaseActivity {

    private FrameLayout mMainFl;
    BaseFragment fg;
    private ImageView mMainLeftImg;
    private ImageView mMainCenterImg;
    private ImageView mMainRightImg;
    private RelativeLayout mMainRl;

    @Override
    public void initParams(Bundle bundel) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(View contextView) {
        mMainFl = contextView.findViewById(R.id.main_fl);
        mMainLeftImg = contextView.findViewById(R.id.main_left_img);
        mMainCenterImg = contextView.findViewById(R.id.main_center_img);
        mMainRightImg = contextView.findViewById(R.id.main_right_img);
        mMainRl = findViewById(R.id.main_rl);
        fg = FragmentFactory.getInstance(MainFg.class);
        if (!fg.isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.main_fl, fg).commit();
        }
    }

    @Override
    protected void setListener() {
        mMainLeftImg.setOnClickListener(this);
        mMainCenterImg.setOnClickListener(this);
        mMainRightImg.setOnClickListener(this);
    }

    @Override
    protected void doBussiness() {

    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.main_left_img:
                if (mMainRl.getVisibility() == View.VISIBLE) {
                    mMainRl.setVisibility(View.GONE);
                }
                switchFragment(fg, FragmentFactory.getInstance(MainFg.class));
                break;
            case R.id.main_center_img:
                if (mMainRl.getVisibility() == View.VISIBLE) {
                    mMainRl.setVisibility(View.GONE);
                } else {
                    mMainRl.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.main_right_img:
//                if (mMainRl.getVisibility() == View.VISIBLE) {
//                    mMainRl.setVisibility(View.GONE);
//                }
//                switchFragment(fg, FragmentFactory.getInstance(MyFg.class));
                startActivity(FansListActivity.class
                );
                break;
        }
    }

    public void switchFragment(BaseFragment from, BaseFragment to) {
        if (from == null || to == null || from == to)
            return;
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!to.isAdded()) {
            transaction.hide(from).add(R.id.main_fl, to).commit();
        } else {
            transaction.hide(from).show(to).commit();
        }
        fg = to;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
